(function () {
    'use strict';

    angular
        .module('app.diagnosis')
        .controller('diagnosisController', diagnosisController);

    diagnosisController.$inject = ['logger', '$state', '$rootScope', '$stateParams','DiagnosisProvider','$mdDialog','$timeout'];
    /* @ngInject */
    function diagnosisController(logger, $state, $rootScope, $stateParams, DiagnosisProvider, $mdDialog, $timeout) {
        var vm = this;
        vm.title = 'diagnosis';
        vm.user = $stateParams.Category;
        vm.password = "";
        vm.ReferralID = $stateParams.ReferralID;
        vm.DisableSave = true;
        vm.onPaginate = onPaginate;
        vm.SerchDiagnosis = SerchDiagnosis;
        vm.SaveDiagnosis = SaveDiagnosis;
        vm.ShowControls = true;

        activate();
        vm.SelectedCodes = [];


        function activate() {
            //vm.TotalDiagnosis = DiagnosisProvider.SetAllDiagnosisList(DefaultListsProvider.GetDiagnosisList());
            //vm.DiagnosisCodes = DiagnosisProvider.GetDiagnosisListForPage(0, 10);
            var promise = DiagnosisProvider.GetPatientDiagnosis(vm.ReferralID).then(function (response) {
                if (response.data.ErrorMessage != 'No Data') {
                    vm.TotalDiagnosis = DiagnosisProvider.SetAllDiagnosisList(response.data.PatientDiagnosis);
                    vm.DiagnosisCodes = DiagnosisProvider.GetDiagnosisListForPage(0, 10);
                    vm.SelectedCodes = response.data.ChipData;
                    $mdDialog.hide();
                }
            });

            //logger.info('Activated diagnosis View');
            
        }

        function onPaginate(page,limit)
        {
            vm.DiagnosisCodes = DiagnosisProvider.GetDiagnosisListForPage(page -1 , limit);
        }

        function SerchDiagnosis() {
            showWait();
            var promise1 = DiagnosisProvider.GetFilteredDiagnosisList(vm.ReferralID, vm.SearchText).then(function (response) {
                vm.TotalDiagnosis = DiagnosisProvider.SetAllDiagnosisList(response.data.PatientDiagnosis);
                vm.DiagnosisCodes = DiagnosisProvider.GetDiagnosisListForPage(0, 10);
                $mdDialog.hide();
            });
        }

        function SaveDiagnosis() {
            showWait();
            var promise2 = DiagnosisProvider.SavePatientDiagnosis(vm.ReferralID, vm.SelectedCodes, vm.DiagnosisNotes)
                .then(function (response) {
                    if (response.data.ErrorMessage == "Saved") {
                        $mdDialog.show(
                              $mdDialog.alert()
                                .parent(angular.element(document.body))
                                .clickOutsideToClose(true)
                                .title('Save Status')
                                .textContent('Patient Diagnosis have been saved.')
                                .ariaLabel('Save Satus Dialog')
                                .ok('Close')
                            ).then(function (response) {
                                $timeout(function () {
                                    angular.element('#BackButton').triggerHandler('click');
                                }, 100);
                            });
                    }
                    $mdDialog.hide();
                });
        }

        function showWait() {
            $mdDialog.show({
                controller: 'diagnosisController',
                template: '<md-dialog style="background-color:transparent;box-shadow:none">' +
                            '<div layout="row" layout-sm="column" layout-align="center center" aria-label="wait">' +
                                '<md-progress-circular md-mode="indeterminate" class="md-accent"></md-progress-circular>' +
                            '</div>' +
                         '</md-dialog>',
                parent: angular.element(document.body),
                clickOutsideToClose: false,
                fullscreen: false
            })
            .then(function (answer) {

            });
        };
    }
})();


